package com.spzx.auth.controller;

import com.spzx.auth.form.LoginBody;
import com.spzx.auth.form.RegisterBody;
import com.spzx.auth.service.H5LoginService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.JwtUtils;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.security.auth.AuthUtil;
import com.spzx.common.security.service.TokenService;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.system.api.model.LoginUser;
import com.spzx.user.api.domain.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * h5 token 控制
 */
@RestController
@Slf4j
public class H5TokenController {
    @Autowired
    private H5LoginService h5LoginService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/h5/register")
    public R<?> register(@RequestBody RegisterBody registerBody) {
        //用户注册
        h5LoginService.register(registerBody);
        return R.ok();
    }

    /**
     * 获取用户登录信息
     *
     * @param form
     * @return
     */
    @PostMapping("/h5/login")
    public R<?> login(@RequestBody LoginBody form) {
        //用户登陆
        LoginUser loginUser = h5LoginService.login(form.getUsername(), form.getPassword());
        //获取登录Token
        Map<String, Object> loginToken = tokenService.createToken(loginUser);
        log.info("******loginToken:::::" + loginToken);
        return R.ok(loginToken);
    }

    /**
     * 用户退出
     * @param request
     * @return
     */
    @DeleteMapping("/h5/logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
        }
        return R.ok();
    }
}